$(document).ready(function(){

	TA.log({id:'coa_57108cf2_298'});  //页面统计

	if($(window).height()<500){

		$(".doubtDisplay").hide();
	}

	$(".doubtDisplay").css("height",$(window).height() - $(".commissionContent").height());

	$(".checkRadio").click(function(event) {
		$(".checkRadio").removeClass("checked");
        $(this).addClass('checked');
        
	});
	$(".commissionContent input").bind('input propertychange', function() {
		if($(".commissionRate input").val() !="" && $(".tradePrice input").val() !="" && $(".tradeNumber input").val() !=""){

			$(".calculation").addClass('change');	
		}
		if($(".commissionRate input").val()=="" || $(".tradePrice input").val()=="" || $(".tradeNumber input").val()==""){

			$(".calculation").removeClass('change');	
		}
    	
	});

	$(".commissionContent input").focus(function(event) {
		$(this).attr("placeholder","");
		
	});
	$(".commissionRate input").blur(function(event) {
		$(this).attr("placeholder","请输入您的佣金率");
	});

	$(".tradePrice input").blur(function(event) {
		$(this).attr("placeholder","请输入您的成交价格");
	});

	$(".tradeNumber input").blur(function(event) {
		$(this).attr("placeholder","请输入您的成交数量");
	});

	$(".calculation").click(function(event) {
      
		TA.log({'id':'coa_57108d34_758'});//计算按钮统计

		if($(".commissionRate input").val()=="" || $(".tradePrice input").val()=="" || $(".tradeNumber input").val()==""){

			return false;
		}
		if($(".tradeNumber input").val()%100 != 0){
			alert("成交数量请输入100的整数倍");
			$(".tradeNumber input").val("");
			$(".calculation").removeClass('change');

		}else if($(".commissionRate input").val() == 0){
			alert("佣金率不能为0，请重新输入");
			$(".commissionRate input").val("");
			$(".calculation").removeClass('change');

		}else if($(".commissionRate input").val() > 30){
			alert("监管规定，佣金率最高为千分之三，请重新输入");
			$(".commissionRate input").val("");
			$(".calculation").removeClass('change');

		}else if($(".tradePrice input").val() > 10000){
			alert("请输入合理的成交价格");
			$(".tradePrice input").val("");
			$(".calculation").removeClass('change');

		}else if($(".tradeNumber input").val() > 1000000000 ){
			alert("成交数量最多支持十亿股，请重新输入");
			$(".tradeNumber input").val("");
			$(".calculation").removeClass('change');

		}else{
			var tradeId = $(".checked").attr("id");
			var commissionRate = $(".commissionRate input").val()/10000;
			var tradePrice = $(".tradePrice input").val();
			var tradeNumber = $(".tradeNumber input").val();
			var displayPrice = (tradePrice*tradeNumber).toFixed(2);  //成交金额
			var commissionPrice = displayPrice*commissionRate;       //佣金
			var stampDuty = (displayPrice*0.001).toFixed(2);        //印花税
			var transferFee = (displayPrice*0.00002).toFixed(2)      //过户费
			if(commissionPrice<5){
				commissionPrice = 5.00;
			}
			$("#commissionPrice span:last-child").html(commissionPrice.toFixed(2));
			$("#displayPrice span:last-child").html(displayPrice);
			$("#transferFee span:last-child").html(transferFee);
			if(tradeId=="tradeBuy"){
				$(".displayDirection span:last-child").html("买入");
		
				$("#stampDuty span:last-child").html("0.00");
				stampDuty = 0.00;

			}else if(tradeId=="tradeSale"){
				$(".displayDirection span:last-child").html("卖出");
				$("#stampDuty span:last-child").html(stampDuty);

			}
			var totalFee = parseFloat(commissionPrice) + parseFloat(stampDuty) + parseFloat(transferFee);  //费用总计
			$("#totalFee span:last-child").html(totalFee.toFixed(2));
			$(".commissionOperation").css("height","auto");
			$(".doubtDisplay").css("display","none");
			$(".calculation").css("margin-bottom","23px");
			$(".commissionDisplay").show();
			
		}
		
	});	
		
});

var commissionRate,tradePrice;
function check(obj,name){
    var num = obj.value;
    if(num){
        if(name == 'commissionRate')commissionRate = num;
        else if(name == 'tradePrice')tradePrice = num;
    }else{
        var beforNum = name == 'commissionRate' ? commissionRate : tradePrice;
        obj.value = beforNum;
        return;
    }
    if(num.length>=2 && num[0]==0 && num[1] != '.'){
        obj.value = num.substring(1);
        return;
    }
    var index = num.indexOf('.');
    if(index > -1){
        var num2 = num.substring(index+1);
        if(num2.length>2){
            obj.value = num.match(/^\d+(?:\.\d{0,2})?/);
        }
    }

}
$('.commissionRate input,.tradePrice input').on('keydown',function(e){
    var num = $(this).val();
    var name = $(this).data('name');
    if(e.keyCode == 8 && num.length == 1){
        if(name == 'commissionRate')commissionRate = '';
        else if(name == 'tradePrice')tradePrice = '';
    }
})
